<template>
	<el-container>
		<div v-show="screenVisible"  class="table-screen-column">
			<el-form label-width="auto" label-position="right">
				<el-row :gutter="20">
				  <el-col :span="4" >
				  	<el-form-item label="联系电话">
						<el-input v-model="search.mobile" placeholder="联系电话" clearable></el-input>
					</el-form-item>
				  </el-col>
				  <el-col :span="4" >
				  	<el-form-item label="问题描述">
						<el-input v-model="search.problem" placeholder="问题描述" clearable></el-input>
					</el-form-item>
				  </el-col>
				</el-row>
			</el-form>
		</div>

		<el-header>
			<div class="left-panel">
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
                <!--v-auth="['manager:users:add']"-->
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-select v-model="search.feedback_type" clearable placeholder="反馈类型" style="width: 120px;">
  					  <el-option v-for="item in feedbackTypeLabel"
  					    :key="item.value" :label="item.label" :value="item.value">
  					  </el-option>
  					</el-select>
					<el-select v-model="search.is_read" clearable placeholder="是否查阅" style="width: 120px;">
  					  <el-option v-for="item in isReadLabel"
  					    :key="item.value" :label="item.label" :value="item.value">
  					  </el-option>
  					</el-select>
					<el-button type="primary" icon="el-icon-search" plain @click="handleQuery"></el-button>
					<el-button @click="resetQuery" icon="el-icon-refresh">重置</el-button>
					<el-button type="primary" link @click="screenVisible = !screenVisible">
            		  {{ screenVisible ? '关闭更多筛选' : '显示更多筛选'}}
            		  <el-icon><el-icon-arrow-down v-if="screenVisible" /><el-icon-arrow-up v-else /></el-icon>
            		</el-button>
				</div>
			</div>
		</el-header>
		
		<el-main class="nopadding">
			<el-table ref="table" v-loading="loading" :data="tableList" @selection-change="selectionChange" border height="100%">
                <el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="#" prop="id" width="100"></el-table-column>
                <el-table-column label="反馈类型" prop="feedback_type">
                    <template #default="scope">
						{{getLableText(scope.row.feedback_type, feedbackTypeLabel)}}
					</template>
                </el-table-column>
                <el-table-column label="联系电话" prop="mobile"></el-table-column>
                <el-table-column label="问题描述" prop="problem"></el-table-column>
                <el-table-column label="是否查阅" prop="is_read">
					<template #default="scope">
						<el-tag v-if="scope.row.is_read==1" type="success">已查阅</el-tag>
						<el-tag v-if="scope.row.is_read==0" type="danger">未查阅</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="创建时间" prop="created_at" width="180"></el-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="170">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>

			</el-table>
		</el-main>
		<el-footer>
			<div v-show="total > 0">
      		  	<el-pagination
      			background small
      			:total="total"
      			:current-page="search.page"
      			:page-size="search.pageSize"
      			layout="total, sizes, prev, pager, next, jumper"
      			@size-change="handleSizeChange"
      			@current-change="handleCurrentChange"
      		  	/>
      		</div>
			<sc-page v-model="search"></sc-page>
		</el-footer>
	</el-container>

	<info-dialog v-if="dialog.info" ref="infoDialog" @success="handleSaveSuccess" @closed="dialog.info=false"></info-dialog>

</template>

<script>
	import infoDialog from './info'

	export default {
		name: 'platformFeedback',
		components: {
			infoDialog,
		},
		data() {
			return {
				dialog: {
					info: false
				},
                url: process.env.VUE_APP_FILES_URL,
				screenVisible: false,
				loading: true,
				tableList: [],
      			total: 0,
				selection: [],
				search: {
					page: 1,
        			pageSize: 20,
					total: 0,
					paging: 0,
        			feedback_type: undefined,
        			is_read: undefined,
        			mobile: undefined,
        			problem: undefined,
				},
				feedbackTypeLabel: [
					{value: 1, label: 'bug反馈'},
					{value: 2, label: '建议'},
					{value: 3, label: '其他'},
				],
				isReadLabel: [
					{value: 0, label: '未查阅'},
					{value: 1, label: '已查阅'},
				],
			}
		},
		mounted() {
			this.handleQuery()
		},
		watch: {
        	"search.paging": function() {
				this.handleQuery()
			},
		},
		methods: {
			//获取列表
			async getTableList() {
				this.loading = true
				var res = await this.$API.platform.feedback.list.get(this.search)
				this.loading = false
				if(res.code == 200) {
					this.tableList = res.data.rows
					this.search.total = res.data.total
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				} 
			},
			//搜索
			handleQuery() {
				this.getTableList()
			},
			//重置搜索
			resetQuery() {
				this.search = {
        			feedback_type: undefined,
        			is_read: undefined,
        			mobile: undefined,
        			problem: undefined,
				}
				this.handleQuery()
			},
			//详情
			table_show(row){
				this.dialog.info = true
				this.$nextTick(() => {
					this.$refs.infoDialog.open('show', this.search).setData(row)
				})
			},
			//删除
			async table_del(row){
				var reqData = {id: row.id}
				var res = await this.$API.platform.feedback.delete.post(reqData);
				if(res.code == 200){
					this.handleQuery()
					this.$message.success("删除成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
					type: 'warning'
				}).then(async() => {
					const loading = this.$loading();
					var dataIds = []
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
						dataIds.push(item.id)
					})
					var res = await this.$API.platform.feedback.delete.post({ids: dataIds});
					if(res.code != 200){
						this.$alert(res.message, "提示", {type: 'error'})
					} else {
						this.$message.success("操作成功")
					}
					this.handleQuery()
					loading.close();
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//本地更新数据
			handleSaveSuccess(mode, search){
				if(mode !='show') {
					this.search = search
					this.handleQuery()
				}
			},
			getLableText(val, lableArr) {
				var txt = ''
				lableArr.forEach(function(item) {
					if(item.value == val) {
						txt = item.label
					}
				})
				return txt
			}
		}
	}
</script>

<style>
/** row **/
.el-row{margin-bottom: 20px;}
.el-row:last-child{margin-bottom: 0;}
.el-col {border-radius: 4px;}
.bg-purple-dark {background: #99a9bf;}
.bg-purple {background: #d3dce6;}
.bg-purple-light {background: #e5e9f2;}
.grid-content{border-radius: 4px;min-height: 36px;}
.row-bg {padding: 10px 0;background-color: #f9fafc;}
</style>
